//
//  SRMyTrunkViewController.m
//  SportRing
//
//  Created by 微他 on 15-1-21.
//  Copyright (c) 2015年 HotOne. All rights reserved.
//

#import "SRMyTrunkViewController.h"
#import "SRMe+Cate.h"
#import "SRDevice+Cate.h"
#import "SRManager.h"
#import "OnlyOnePickerView.h"

@interface SRMyTrunkViewController ()
{
    OnlyOnePickerView *_pickerView;
    UIButton *_handBtn;
}
@property (strong, nonatomic) IBOutlet UILabel *label;
@property (strong, nonatomic) IBOutlet UIView *removeBund;
@property (strong, nonatomic) IBOutlet UILabel *bundDeviceName;
@property (strong, nonatomic) IBOutlet UIButton *removeBundBtn;

@end

@implementation SRMyTrunkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.navigationItem.hidesBackButton = YES;
    [self setCustomTitle:NSLocalizedString(@"我的手环", nil)];
    [self addBackButton];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(setupData)
                                                 name:@"didFindPeripheral"
                                               object:nil];
    [self initView];
    [self setupData];
}

- (void)initView
{
    _pickerView = [[OnlyOnePickerView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_label.frame) + 5, CGRectGetWidth(self.view.bounds), 260)];
    //    pickerView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_pickerView];
    
    _handBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_handBtn setTitle:@"绑定" forState:UIControlStateNormal];
    [_handBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_handBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
    _handBtn.backgroundColor = [UIColor colorWithRed:144.0/255.0 green:87.0/255.0 blue:192.0/255.0 alpha:1];
    _handBtn.layer.cornerRadius = 6;
    _handBtn.frame = CGRectMake(15, CGRectGetMaxY(_pickerView.frame) + 5, CGRectGetWidth(self.view.bounds) - 30, 40);
    [_handBtn addTarget:self action:@selector(boundHandRing) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_handBtn];
    _handBtn.hidden = YES;
}

- (void)setupData
{
    NSMutableArray *boundedHandRingArray = [NSMutableArray new];
    
    SRMe * me = [SRMe currentUser];
    SRDevice * device = me.device;
    
    if (device && device.name) {
        NSString *name = device.name;
        [boundedHandRingArray addObject:name];
        _label.hidden = YES;
        _bundDeviceName.text = name;
        _pickerView.hidden = YES;
        _handBtn.hidden = YES;
        _removeBund.hidden = NO;
//        _pickerView.selectPerName = name;
//        [self bundDeviceName];
    } else
    {
        _label.hidden = NO;
        _removeBund.hidden = YES;
        _handBtn.hidden = NO;
        _pickerView.hidden = NO;
        
//        if (_pickerView.selectPerName.length > 0) {
//            SRDevice * device = [SRDevice findDeviceCINEForMe:[SRMe currentUser]];
//            [device configAttributesWithBlock:^(SRDevice *deviceSelf) {
//                deviceSelf.name = _pickerView.selectPerName;
//            }];
//            [[SRManager sharedInstance] setCurrentPeripheralName:_pickerView.selectPerName];
//        }
    }
}

- (IBAction)remobundClick:(id)sender {
    [self removeHandRing];
}
- (IBAction)pushHelp:(id)sender {
    [self performSegueWithIdentifier:@"pushHelp" sender:self];
}

- (void)removeHandRing
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"sleep"];
    [[SRManager sharedInstance] disconnectWithDevice];
    SRDevice * device = [SRDevice findDeviceForMe:[SRMe currentUser]];
    [SRDevice deleteDevice:device];
    [self setupData];
}

- (void)boundHandRing
{
    SRDevice * device = [SRDevice findDeviceForMe:[SRMe currentUser]];
    [SRDevice deleteDevice:device];
    device = [SRDevice findDeviceCINEForMe:[SRMe currentUser]];
    [device configAttributesWithBlock:^(SRDevice *deviceSelf) {
        deviceSelf.name = _pickerView.selectPerName;
    }];
    [[SRManager sharedInstance] setCurrentPeripheralName:_pickerView.selectPerName];
    [self setupData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
